<?php

/**
 * Script used to update projects and projectrequest in the dataase, whenever
 * en employee assigns a project to a studentgroup.
 */
// Start the session handling system
session_start();

// Connect to the database
require_once ("../../../db.php");

// Only allow this for employees
if (!isset($_SESSION['uid']) || ($_SESSION['type'] != 'employee'))
    die('Not logged in as an employee user');

// update projects
$sql = 'UPDATE projects SET status="given", advisor1=?, advisor2=?  WHERE id=?';    
$sth = $db->prepare($sql);
$sth->execute(array($_POST['adv1'], $_POST['adv2'], $_POST['proid']));

// update projectrequest
$sql = 'UPDATE projectrequest SET priority="taken" WHERE projectid=? AND groupid=?';    
$sth = $db->prepare($sql);
$sth->execute(array($_POST['proid'], $_POST['grpid']));

$sql = 'SELECT e.email, g.participantid, p.title, t.name FROM externalusers e, projects p, groupparticipants g, projectgroups t
WHERE p.id ='.$_POST['proid'].' AND e.id = p.owner AND g.groupid='.$_POST['grpid'].' AND t.id='.$_POST['grpid'];
$sth = $db->prepare($sql);
$sth->execute(array($_POST['grpid']));
$row = $sth->fetchAll();
if($row) {
    echo json_encode($row);
} else {
    echo json_encode(array('error' => 'Ingen gruppemedlemmer funnet'));
}

?>
